﻿/////////////////////////////////////////////////////////////////////////////////
//
// PlainText
// - Gets rid of unwanted formatting
// (C) Copyright Johan Åhlén 2009
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
// See the GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/.
//
// More info on PlainText website: http://plaintext.codeplex.com
//
/////////////////////////////////////////////////////////////////////////////////


using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

namespace AdvantIQ.PlainText
{
    /// <summary>
    /// This class helps enabling/disabling autostart of an application through setting the appropriate registry keys.
    /// </summary>
    internal static class Autostart
    {
        /// <summary>
        /// Registry location for autostart
        /// </summary>
        private const string RUN_LOCATION = @"Software\Microsoft\Windows\CurrentVersion\Run";

        /// <summary> 
        /// Sets the autostart value for the assembly. 
        /// </summary> 
        /// <param name="keyName">Registry Key Name</param> 
        /// <param name="assemblyLocation">Assembly location (e.g. Assembly.GetExecutingAssembly().Location)</param> 
        public static void SetAutoStart(string keyName, string cmd)
        {
            RegistryKey key = Registry.CurrentUser.CreateSubKey(RUN_LOCATION);
            key.SetValue(keyName, cmd);
        }

        /// <summary> 
        /// Returns whether auto start is enabled. 
        /// </summary> 
        /// <param name="keyName">Registry Key Name</param> 
        /// <param name="assemblyLocation">Assembly location (e.g. Assembly.GetExecutingAssembly().Location)</param> 
        public static bool IsAutoStartEnabled(string keyName, string assemblyLocation)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(RUN_LOCATION);
            if (key == null) 
                return false;
            var value = (string)key.GetValue(keyName);
            if (value == null) 
                return false;
            return value.Contains(assemblyLocation);
        }

        /// <summary> 
        /// Unsets the autostart value for the assembly. 
        /// </summary> 
        /// <param name="keyName">Registry Key Name</param> 
        public static void UnSetAutoStart(string keyName)
        {
            RegistryKey key = Registry.CurrentUser.CreateSubKey(RUN_LOCATION);
            key.DeleteValue(keyName);
        }
    }
}
